package com.burgeon.backend.SHOPBACKEND.service.Impl;

import com.burgeon.backend.SHOPBACKEND.entity.Goods;
import com.burgeon.backend.SHOPBACKEND.entity.GroupData;
import com.burgeon.backend.SHOPBACKEND.mappers.BackendGoodsMapper;
import com.burgeon.backend.SHOPBACKEND.mappers.BackendGroupMapper;
import com.burgeon.backend.SHOPBACKEND.service.BackendGroupService;
import com.burgeon.backend.SHOPBACKEND.service.ex.ErrorDataException;
import com.burgeon.backend.SHOPBACKEND.service.ex.UploadException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class BackendGroupServiceImpl implements BackendGroupService {
    @Autowired
    private BackendGroupMapper backendGroupMapper;
    @Autowired
    private BackendGoodsMapper backendGoodsMapper;

    @Override
    public String getImg(String account) {
        return backendGroupMapper.findImg(account)+"?"+((Math.random() * 9 + 1) * 100000);
    }

    @Override
    public String upImg(MultipartFile file, String account) throws ErrorDataException {
        if (null == file || file.isEmpty()) {
            System.out.println("文件为空");
            throw new UploadException("请上传新图片");
        }

        String fileName = file.getOriginalFilename(); //文件名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));// 后缀名

        String filePath = "//root//code//img//" + account + "-shop//group//"; // 上传后的路径
        fileName = "group" + suffixName; // 新文件名


        File dest = new File(filePath + fileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }

        try {
            file.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
            throw new ErrorDataException("系统错误");
        } catch (Exception e) {
            System.err.println(e.getMessage());
            throw new ErrorDataException("系统错误");
        }
        String img = "http://lfwdcdn.cloudsmicro.com/img/" + account + "-shop/group/" + fileName;

        if (null == backendGroupMapper.findImg(account)){
            backendGroupMapper.addImg(img,account);
        }else {
            backendGroupMapper.upImg(img,account);
        }

        return img;
    }

    @Override
    public void isUse(String id, String use) throws ErrorDataException{
        Integer row = backendGroupMapper.upUse(id, use);
        if (row == 0) {
            throw new ErrorDataException("出现错误");
        }
    }

    @Override
    public HashMap getItem(String id) {
        GroupData group = backendGroupMapper.findDataById(id);

        Goods goods = backendGoodsMapper.getGoodsById(group.getGid());

        HashMap data = new HashMap();
        data.put("group",group);
        data.put("goods",goods);

        return data;
    }

    @Override
    public void deleteGroup(String id, String account) throws ErrorDataException {
        int row = backendGroupMapper.delGroupData(id, account);
        if (row == 0) {
            throw new ErrorDataException("出现错误");
        }
    }

    @Override
    public List<GroupData> getData(String account) {
        return backendGroupMapper.getGroupList(account);
    }

    @Override
    public void createGroup(GroupData groupData) throws ErrorDataException {
        if(null!=backendGroupMapper.getGroupByGid(groupData.getGid())){
            throw new ErrorDataException("商品已存在");
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String now = sdf.format(new Date());
        groupData.setCreateTime(now);
        groupData.setGid(backendGoodsMapper.getGidByCode(groupData.getCode(),groupData.getAccount()));

        int row = backendGroupMapper.addGroupData(groupData);
        if (row == 0) {
            throw new ErrorDataException("出现错误");
        }
        backendGoodsMapper.upGoodsState(groupData.getGid(),"N");
    }

    @Override
    public void underGroup() {
        List<GroupData> groupDatas = backendGroupMapper.getGroupData();
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String newDate = sf.format(date);
        Date d = null;
        try {
            d = sf.parse(newDate);
            for (GroupData groupData:groupDatas) {
                if(groupData.getUnderTime() != null) {
                    if(sf.parse(groupData.getUnderTime()).before(d)){
                       int r =  backendGroupMapper.upUse(groupData.getId(),"N");
                        System.err.println(r);
                    }

                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }


}
